/// created by hujintao
/// created at 2023/8/17
/// 描述:

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:get/get.dart';

class HKOverlay {
  factory HKOverlay() => _instance;
  static final HKOverlay _instance = HKOverlay._internal();

  static HKOverlay get instance => _instance;
  OverlayEntry? overlayEntry;

  HKOverlay._internal() {
    overlayEntry = OverlayEntry(
      builder: (context) => Stack(
        children: [
          // 遮罩层
          ModalBarrier(
            dismissible: false,
            color: Colors.black.withOpacity(0.5),
          ),
          // 加载中的提示
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: 100.w,
                  height: 100.w,
                  decoration: BoxDecoration(
                      color: Colors.black,
                      borderRadius: BorderRadius.circular(6.w)),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text(
                        '上传中...',
                        style: TextStyle(color: Colors.white, fontSize: 16.sp),
                      ),
                      SizedBox(
                        height: 8.w,
                      ),
                      // const CircularProgressIndicator(
                      //   backgroundColor: Color(0xFFED5130),
                      //   color: Colors.white,
                      // ),
                      const SpinKitThreeBounce(
                        size: 28,
                        color: Colors.white,
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  void showOverlay() {
    if (Get.context != null) {
      Overlay.of(Get.context!)?.insert(
        overlayEntry!,
      );
    }
  }

  void hideOverlay() {
    if (Get.context != null && overlayEntry != null) {
      try {
        overlayEntry?.remove();
      } catch (e) {
        print('hideOverlayError:$e');
      }
    }
  }
}
